import types from "../action-types"

const isLogin = Boolean(window.localStorage.getItem("authToken")) || Boolean(window.sessionStorage.getItem("authToken"));
const userInfo = window.localStorage.getItem("userInfo") || window.sessionStorage.getItem("userInfo");

export interface IUserState {
    isLoading: boolean
    isLogin: boolean
    authToken: boolean
    token: string
    userInfo: any
}


const initState: IUserState = {
    isLoading: false,
    isLogin: false,
    authToken: false,
    token: '',
    userInfo: {}
};


export default (state = initState, action: any) => {

    switch (action.type) {
        case types.START_LOGIN:
            return {
                ...state,
                authToken: false,
                token: '',
                isLoading: true,
                isLogin: false,

            };
        case types.LOGIN_SUCCESS:
            return {
                ...state,
                ...action.payload.userInfo,
                isLoading: false,
                isLogin: true,
                authToken: true,
                token: action.token,
            };
        case types.LOGIN_FAILED:
            return {
                ...state,
                isLoading: false,
                isLogin: false
            };
        default:
            return state;

    }

}
